package com.learn.grpc.hello;

import com.learn.grpc.grpc.HelloServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

@Slf4j
public class HelloServiceGrpcClient {

    @Test
    public void unaryMethod() {
        //1.构建网络通道
        ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost", 9091)
                .usePlaintext()
                .build();

        //2.构建客户端
        HelloServiceGrpc.HelloServiceBlockingStub stub = HelloServiceGrpc.newBlockingStub(managedChannel);

        //3.发送多次请求
        for (int i = 0; i < 5; i++) {
            PingRequest request = PingRequest.newBuilder()
                    .setContent("zhangsan_" + i)
                    .build();
            PingResponse resp = stub.ping(request);

            log.info("code:{}, message:{}, data:{}", resp.getCode(), resp.getMessage(), resp.getData());
        }
    }

}
